package javaCode.chapter13.homework;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author 徐州城
 * @version 1.0
 */
@SuppressWarnings("all")
public class Homework02 {
    public static void main(String[] args) {
        /*
        使用ArrayList 完成对 对象 Car{name, price} 的各种操作
        1. add：添加单个元素
        2. remove：删除指定元素
        3. contains：查找元素是否存在
        4. size：获取元素个数
        5. clear：清空
        6. isEmpty：判断是否为空
        7. addAll：添加多个元素
        8. containsAll：查找多个元素是否都存在
        9. removeAll：删除多个元素
        使用增强for和 迭代器来遍历所有的car，需要重写Car 的toString方法
         */
        List list = new ArrayList();
        Car car1 = new Car("宝马", 150000.0);
        Car car2 = new Car("奥迪", 300000.0);
        list.add(car1);
        list.add(car2);
        System.out.println("car1是否存在" + list.contains(car1));

        System.out.println("list的元素个数=" + list.size());

        //list.clear();

        System.out.println("list是否为空=" + list.isEmpty());

        ArrayList arrayList = new ArrayList();
        arrayList.add("java");
        list.addAll(arrayList);
        System.out.println("addAll后的元素=" + list);

        boolean b = list.containsAll(arrayList);
        System.out.println("containsAll的结果=" + b);

        list.removeAll(arrayList);

        System.out.println("增强for");
        for (Object o : list) {
            System.out.println("list=" + o);
        }

        System.out.println("迭代器");
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println("list=" + next);
        }
    }
}

class Car{
    private String name;
    private double price;

    public Car(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}